<?php
class Model_WotHub {
    
    protected $_pdo=null;
    protected $_appId= null;
    private static $_baseUri = 'http://api.worldoftanks.eu/wot/account/';
    
    public function __construct($pdo, $appId) {
        $this->_pdo = $pdo;
        $this->_appId = $appId;
    }
    
    public function executerSql($sql, $bind= null, $nomSequence= null) {
        $pdo = $this->_pdo;
        return $pdo->executerSql($sql, $bind, $nomSequence);   
    }
    
    public static function api_get_content($url, $fichier, $lang= true) {
        $cible = $url;
        $url.='&access_token='.$_SESSION['access_token'];
        if ($lang === true) {
            $cible = $url.'&language=fr';
        }
        if($_SERVER['HTTP_HOST'] === 'localhost') {
            $cible = 'http://localhost/json/'.$fichier;
        }
        return file_get_contents($cible);
    }
    
    public static function getStatAccueil() {
        global $application;
        $pdo = new smartbokModele($application['bdd']);
        
        $statAccueil['clans'] = $pdo->executerSql('SELECT COUNT(DISTINCT(clan_id)) nbClan FROM clan');
        $statAccueil['membres'] = $pdo->executerSql('SELECT COUNT(DISTINCT(account_id)) nbMembre FROM garage');
        $statAccueil['events'] = $pdo->executerSql('SELECT COUNT(agenda_id) nbEvent FROM agenda');

        return $statAccueil;
    }
    
    public static function getOpenAgenda() {
        global $application;
        $pdo = new smartbokModele($application['bdd']);
        
        $sql = 'SELECT a.date, d.titre, c.nom_clan, e.nom, e.image
                FROM agenda a
                JOIN agenda_detail d ON a.agenda_id = d.agenda_id AND  d.visibilite = 2 AND event_id=10
                JOIN events e ON e.event_id = a.event_id
                LEFT join clan c ON c.clan_id = a.clan_id
                WHERE a.date >= :date
                ORDER BY a.date LIMIT 6';
        $dateDebut = date('Y').date('m').date('d');
        $events = $pdo->executerSql($sql, array(':date' => $dateDebut));
        return $events;
    }
}